package org.pexx.rdm.utils;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TimeChartPanel extends javax.swing.JPanel {

	private String title;
	private String timeAxisLabel;
	private String valueAxisLabel;
	private TimeSeriesCollection dataset;
	private JFreeChart chart;
	
	private HashMap <String, TimeSeries> series;
	private long maxItemAge = 1000;
	
	/**
	 * @param title
	 * @param timeAxisLabel
	 * @param valueAxisLabel
	 */
	public TimeChartPanel(String title, String timeAxisLabel,
			String valueAxisLabel) {
		
		super();

		this.title = title;
		this.timeAxisLabel = timeAxisLabel;
		this.valueAxisLabel = valueAxisLabel;
		
		initData();
		
		initGUI();
	}

	public TimeChartPanel() {
		this ("TimeSeries", "Time", "Value");
	}
	
	/**
	 * 
	 */
	private void initData() {
		
		series = new HashMap<String, TimeSeries>();
		dataset = new TimeSeriesCollection();
		
		
	}

	/**
	 * 
	 */
	private void initChart() {

		chart = ChartFactory.createTimeSeriesChart(title, timeAxisLabel, valueAxisLabel, dataset, true, true, false);
		
		getRenderer().setStroke(new BasicStroke(2f));
	}
	
	
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {

		this.title = title;
		chart.setTitle(title);
	}

	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			
			initChart();
			
							
			this.add(new ChartPanel(chart), BorderLayout.CENTER);
			
			
			
			this.setPreferredSize(new java.awt.Dimension(640,480));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public XYPlot getPlot () {
		return ((XYPlot) chart.getPlot());
	}
	
	public XYItemRenderer getRenderer() {
		return getPlot().getRenderer();
	}
	
	
	/**
	 * @return the dataset
	 */
	public TimeSeriesCollection getDataset() {

		return dataset;
	}
	
	public TimeSeries addSeries (String id) {
		TimeSeries ts = new TimeSeries(id);
		
		ts.setMaximumItemAge(maxItemAge);
		
		series.put(id, ts);
		dataset.addSeries(ts);
		
		getRenderer().setSeriesPaint(dataset.getSeriesCount()-1, new Color (id.hashCode()));
		
		return ts;
	}
	
	public void removeSeries (String id) {
		dataset.removeSeries(series.remove(id));
	}
	
	public Set<String> getIds() {
		return series.keySet();
	}

	public void addOrCreateValue (String id, Date time, int value) {
		
		if (!series.containsKey(id))		
			addSeries(id);
		
		//System.out.println("adding " + value);
		series.get(id).addOrUpdate(new Second (time), value);
	}

	public void addOrCreateValue (String id, Date time, double value) {
		
		if (!series.containsKey(id))		
			addSeries(id);
		
		series.get(id).addOrUpdate(new Second (time), value);
	}
	
	public void setMaxItemAge (String id, long numValues) {
		series.get(id).setMaximumItemAge(numValues);
	}
	
	public void setMaxItemAge (long maxItemAge) {
		this.maxItemAge = maxItemAge;
		
		for (TimeSeries t : series.values())
			t.setMaximumItemAge(maxItemAge);
	}	
	
	/**
	 * @return the maxItemAge
	 */
	public long getMaxItemAge() {

		return maxItemAge;
	}
	
}
